unit NetworkUtils;

interface

uses
  Windows;

function  ConnectNetShareFolder(FullPath:String; APassword, AUser:String):Boolean;

implementation

uses
  siAuto;

function  ConnectNetShareFolder(FullPath:String; APassword, AUser:String):Boolean;
var
  n : NETRESOURCE;
  iRet : integer;
  ErrorMessage:String;
begin
  Result := False;
  n.dwScope:=RESOURCE_GLOBALNET;
  n.dwType:=RESOURCETYPE_DISK;
  n.dwDisplayType:=RESOURCEDISPLAYTYPE_GENERIC;
  n.dwUsage:=RESOURCEUSAGE_CONNECTABLE;
  n.lpLocalName:='';
  n.lpRemoteName:=PChar(FullPath);
  n.lpComment:='';
  n.lpProvider:='';
  iRet:=WNetAddConnection2(n,PChar(APassword),PChar(AUser),0);
  case iRet of
    NO_ERROR                          : begin
                                          Result := True;
                                          sleep(500);
                                        end;
    ERROR_ACCESS_DENIED	              : ErrorMessage:='Access to the network resource was denied.';
    ERROR_ALREADY_ASSIGNED	          : ErrorMessage:='The local device specified by lpLocalName is already connected to a network resource.';
    ERROR_BAD_DEV_TYPE	              : ErrorMessage:='The type of local device and the type of network resource do not match.';
    ERROR_BAD_DEVICE	                : ErrorMessage:='The value specified by lpLocalName is invalid.';
    ERROR_BAD_NET_NAME	              : ErrorMessage:='The value specified by lpRemoteName is not acceptable to any network resource provider. The resource name is invalid, or the named resource cannot be located.';
    ERROR_BAD_PROFILE	                : ErrorMessage:='The user profile is in an incorrect format.';
    ERROR_BAD_PROVIDER	              : ErrorMessage:='The value specified by lpProvider does not match any provider.';
    ERROR_BUSY	                      : ErrorMessage:='The router or provider is busy, possibly initializing. The caller should retry.';
    ERROR_CANCELLED	                  : ErrorMessage:='The attempt to make the connection was cancelled by the user through a dialog box from one of the network resource providers, or by a called resource.';
    ERROR_CANNOT_OPEN_PROFILE	        : ErrorMessage:='The system is unable to open the user profile to process persistent connections.';
    ERROR_DEVICE_ALREADY_REMEMBERED	  : ErrorMessage:='An entry for the device specified in lpLocalName is already in the user profile.';
    ERROR_EXTENDED_ERROR	            : ErrorMessage:='A network-specific error occured. Call the WNetGetLastError function to get a description of the error.';
    ERROR_INVALID_PASSWORD	          : ErrorMessage:='The specified password is invalid.';
    ERROR_NO_NET_OR_BAD_PATH	        : ErrorMessage:='A network component has not started, or the specified name could not be handled.';
    ERROR_NO_NETWORK	                : ErrorMessage:='There is no network present.';
    else                                ErrorMessage:='An unknown error has occured attempting to connect to ' + FullPath;
  end;

  if not Result then
    simain.LogFatal('[%s] Network Connection Fail. %s', [FullPath, ErrorMessage]);

end;


end.
